package com.xkdx.guangguang.fragment.brand.branch;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.shareclass.DiscountInfo;
import com.xkdx.guangguang.shareclass.ProductInfo;
import com.xkdx.guangguang.shareclass.ShopCommentary;
import com.xkdx.guangguang.shareclass.ShopDetail;

public class BranchModule extends AbsModule {
	ShopDetail shopDetail;
	List<DiscountInfo> newsList;
	List<ShopCommentary> shopCommenList;
//	List<ProductInfo> unitList;

	@Override
	public void parseData() throws Exception {
		backJson = (JSONArray) result;

		try {
			for (int i = 0; i < backJson.length(); i++) {
				jsonObj = (JSONObject) backJson.get(i);
				String sortOfAct = jsonObj.getString("Act");
				if (sortOfAct.equals("getShopDetail")) {
					shopDetail = parseShopInfoDetail(jsonObj
							.getJSONObject("Content"));
				} else if (sortOfAct.equals("getProductList")) {
					// unitList = parseUnitInfoDetail(jsonObj
					// .getJSONObject("Content"));
				} else if (sortOfAct.equals("getInfoListByShop")) {
					newsList = parseDiscountInfo(jsonObj
							.getJSONObject("Content"));
				} else {
					shopCommenList = parseAllCommentInfo(jsonObj
							.getJSONObject("Content"));
				}

			}
		} catch (Exception e) {
			throw e;
		}

	}

//	/**
//	 * 单品特卖
//	 * 
//	 * @param jsonObject
//	 * @return
//	 * @throws Exception
//	 */
//	private List<ProductInfo> parseUnitInfoDetail(JSONObject jsonObject)
//			throws Exception {
//		List<ProductInfo> list = new ArrayList<ProductInfo>();
//
//		try {
//			JSONObject stut = (JSONObject) jsonObject.get("Result");
//			JSONArray ListInfo = (JSONArray) stut.getJSONArray("DetailInfo");
//			JSONObject time = stut.getJSONObject("ListInfo");
//			for (int i = 0; i < ListInfo.length(); i++) {
//				JSONObject jsonObj = ((JSONObject) ListInfo.opt(i));
//				ProductInfo unitInfo = new ProductInfo();
//				unitInfo.setProductID(jsonObj.getString("ID"));
//				unitInfo.setProductName(jsonObj.getString("Product_Name"));
//				unitInfo.setExChange_Num(jsonObj.getString("ExChangeNum"));
//				unitInfo.setRemain_Num(jsonObj.getString("Remain_Num"));
//				unitInfo.setLogo(jsonObj.getString("Logo"));
//				unitInfo.setProductPrice(jsonObj.getString("Product_Price"));
//				list.add(unitInfo);
//			}
//
//		} catch (Exception e) {
//
//			throw e;
//
//		}
//
//		return list;
//	}

	// 解析评论详情内容
	private List<ShopCommentary> parseAllCommentInfo(JSONObject person)
			throws Exception {
		List<ShopCommentary> list = new ArrayList<ShopCommentary>();
		try {
			JSONObject stut = (JSONObject) person.get("Result");
			JSONArray ListInfo = (JSONArray) stut.getJSONArray("ListInfo");
			for (int i = 0; i < ListInfo.length(); i++) {
				JSONObject jsonObj = ((JSONObject) ListInfo.opt(i));
				ShopCommentary commentinfo = new ShopCommentary();
				commentinfo.setCommentContent(jsonObj
						.getString("CommentContent"));
				commentinfo.setNickName(jsonObj.getString("NickName"));
				commentinfo.setCreateTime(jsonObj.getString("CreateTime"));
				commentinfo.setPictrueUrl(jsonObj.getString("PictureUrl"));
				commentinfo.setUserAvatars(jsonObj.getString("UserAvatars"));
				list.add(commentinfo);
			}

		} catch (Exception e) {
			throw e;
		}

		return list;
	}

	private ShopDetail parseShopInfoDetail(JSONObject person) throws Exception {

		ShopDetail detail = new ShopDetail();
		List<String> list = new ArrayList<String>();
		try {
			JSONObject stuts = (JSONObject) person.get("Result");
			JSONObject item = stuts.getJSONObject("DetailInfo");

			detail.setAddress(item.getString("Address"));
			detail.setIntro(item.getString("Intro"));
			detail.setLatitude(item.getString("Latitude"));
			detail.setLogo(item.getString("Logo"));
			detail.setLongitude(item.getString("Longitude"));
			detail.setSimpleintro(item.getString("SimpleIntro"));
			detail.setShopID(item.getString("ShopID"));
			detail.setShopName(item.getString("ShopName"));
			detail.setWebSiteUrl(item.getString("WebSiteUrl"));
			detail.setDiscountCount(item.getString("DiscountCount"));
			detail.setAttentionCount(item.getString("AttentionCount"));
			detail.setChildShopCount(item.getString("ChildShopCount"));
			detail.setCommentCount(item.getString("CommentCount"));
			detail.setNewProductCount(item.getString("NewProductCount"));
			detail.setPhone(item.getString("Phone"));
			detail.setFloor(item.getString("Floor"));
			detail.setShopNo(item.getString("ShopNo"));
			detail.setSignInCount(item.getString("SignInCount"));
			detail.setShopType(item.getString("ShopType"));
			detail.setWeibo(item.getString("Weibo"));
			detail.setDianpingShopID(item.getString("DianpingShopID"));
			detail.setIcon1(item.getString("Icon1"));
			detail.setIcon2(item.getString("Icon2"));
			detail.setIcon3(item.getString("Icon3"));

			JSONArray array = item.getJSONArray("PictureList");
			for (int i = 0; i < array.length(); i++) {
				String l = (String) array.optString(i);
				list.add(l);
			}
			detail.setPictureList(list);

		} catch (Exception e) {
			throw e;
		}

		return detail;
	}

	private List<DiscountInfo> parseDiscountInfo(JSONObject person)
			throws Exception {
		List<DiscountInfo> list = new ArrayList<DiscountInfo>();

		try {
			JSONObject stut = (JSONObject) person.get("Result");
			JSONArray ListInfo = (JSONArray) stut.getJSONArray("ListInfo");
			JSONObject time = stut.getJSONObject("DetailInfo");
			for (int i = 0; i < ListInfo.length(); i++) {
				JSONObject jsonObj = ((JSONObject) ListInfo.opt(i));
				DiscountInfo discountInfo = new DiscountInfo();
				discountInfo.setInfoID(jsonObj.getString("InfoID"));
				discountInfo.setInfoTitle(jsonObj.getString("InfoTitle"));
				discountInfo.setInfoType(jsonObj.getString("InfoType"));
				discountInfo.setBeginTime(jsonObj.getString("BeginTime"));
				discountInfo.setEndTime(jsonObj.getString("EndTime"));
				discountInfo.setPictrueUrl(jsonObj.getString("PictureUrl"));
				list.add(discountInfo);

			}

		} catch (Exception e) {
			throw e;
		}

		return list;

	}

}
